package edu.neusoft.yiqibike.common.entity.convert;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * User：lyc
 * Date：2017-12-29
 * Time：20:55
 * Description：
 */
public class Converter<U, T> {
    private final Function<T, U> toDto;
    private final Function<U, T> toEntity;

    public Converter(final Function<T, U> toDto, final Function<U, T> toEntity) {
        this.toDto = toDto;
        this.toEntity = toEntity;
    }

    public final U toDto(final T t) {
        return this.toDto.apply(t);
    }

    public final T toEntity(final U u) {
        return this.toEntity.apply(u);
    }

    public final List<U> toDtoList(final Collection<T> t) {
        return t.stream().map(this::toDto).collect(Collectors.toList());
    }

    public final List<T> toEntityList(final Collection<U> u) {
        return u.stream().map(this::toEntity).collect(Collectors.toList());
    }
}
